package com.gopay.bis.cib.ebank.operation;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.gopay.bis.cib.ebank.transformer.CibQueryClientTransformer;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.TransUtils;
import com.gopay.common.constants.bank.CibConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.cib.ebank.CibMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;

/**
 * 兴业B2C订单查询处理类
 * 
 * @CibB2cQuery.java
 * @author zhengxue
 * @2014-11-20 下午3:39:27 www.gopay.com.cn Inc.All rights reserved.
 */
public class CibB2cQuery {
    // 签名验签类
    private ICertHelper cibB2cCertHelper;
//    // 通讯类
//    private CibQueryTransformer httpsTransport;

    public Object query(CibMessage message) throws GopayException {
        try {
            String signature = cibB2cCertHelper.sign(message);
            message.setMac(signature);
            Map<String, String> reqMsg = buildReqData(message);
            BankLogUtils.printB2CQuerySendLog(message, reqMsg);

            CibQueryClientTransformer httpsTransport = new CibQueryClientTransformer();
            Object obj = (Object) httpsTransport.submit(message.getQueryUrl(), reqMsg);
            BankLogUtils.printB2CQueryBlackLog(message, obj);
            return parseQueryRes(obj, message);
        } catch (Exception e) {
            BankLogUtils.printB2CQueryExceptionLog(message, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6037);
        }
    }

    /**
     * 组装订单查询请求数据
     * 
     * @param message
     * @return
     */
    private Map<String, String> buildReqData(CibMessage message) {
        Map<String, String> reqData = new HashMap<String, String>();
        reqData.put("service", message.getService());
        reqData.put("ver", message.getVer());
        reqData.put("mrch_no", message.getMrchNo());
        reqData.put("ord_no", message.getOrderId());
        reqData.put("mac", message.getMac());
        return reqData;
    }

    private CibMessage parseQueryRes(Object obj, CibMessage message) {
        String resString = "";
        if (obj instanceof byte[]) {
            resString = new String((byte[]) obj);
        } else if (obj instanceof String) {
            resString = (String) obj;
        }

        if (resString.contains(CibConstants.NETPAYERR_BEGIN_TAG)) {
            // 查询失败
            // 错误码
            String code = TransUtils.getMsgField(CibConstants.CODE_BEGIN_TAG, CibConstants.CODE_END_TAG, resString);
            // 错误信息
            String msg = TransUtils.getMsgField(CibConstants.MSG_BEGIN_TAG, CibConstants.MSG_END_TAG, resString);
            message.setCode(code);
            message.setMsg(msg);
        }

        if (resString.contains(CibConstants.NETPAYRESP_BEGIN_TAG)) {
            // 订单号
            String orderNo = TransUtils.getMsgField(CibConstants.ORDERNO_BEGIN_TAG, CibConstants.ORDERNO_END_TAG,
                    resString);
            // 订单金额
            String orderAmount = TransUtils.getMsgField(CibConstants.ORDERAMOUNT_BEGIN_TAG,
                    CibConstants.ORDERAMOUNT_END_TAG, resString);
            // 订单状态
            String orderStatus = TransUtils.getMsgField(CibConstants.ORDERSTATUS_BEGIN_TAG,
                    CibConstants.ORDERSTATUS_END_TAG, resString);
            message.setOrderId(orderNo);
            message.setAmount(new BigDecimal(orderAmount));
            message.setOrderStatus(orderStatus);
        }
        return message;
    }

    public void setCibB2cCertHelper(ICertHelper cibB2cCertHelper) {
        this.cibB2cCertHelper = cibB2cCertHelper;
    }

//    public void setHttpsTransport(CibQueryTransformer httpsTransport) {
//        this.httpsTransport = httpsTransport;
//    }

}
